package cl.query.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

import cl.model.dto.Vehicle;
import cl.parsers.Parser;
import cl.parsers.ParserVehicle;


/**
 * Servlet implementation class CreaQueryServlet
 */
public class GetVehicleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(GetVehicleServlet.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetVehicleServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		String uri = "http://www2.chileautos.cl/";
		String codVehicle = "auto.asp?codauto=1779742";
	
		String salidaXML = new String();	

		HttpClient client = new HttpClient( );
		String url = uri + codVehicle;
		HttpMethod method = new GetMethod( url );

		try {
		    client.executeMethod( method );
		    if( method.getStatusCode( ) == HttpStatus.SC_OK ) {
		        salidaXML = method.getResponseBodyAsString( );
		        System.out.println( salidaXML );
		    }
		} catch( HttpException he ) {
		    System.out.println( "HTTP Problem: " + he.getMessage( ) );
		} catch( IOException ioe ) {
		    System.out.println( "IO Exeception: " + ioe.getMessage( ) );
		} finally {
		    method.releaseConnection( );
		    method.recycle( );
		}	
		
		int tablaPrincipal = salidaXML.indexOf("class='X3'");
		
		if(tablaPrincipal != -1){

			int indiceTablaPrincipal = tablaPrincipal - 18;
			int indiceFinalTablaPrincipal = salidaXML.indexOf("</table>",indiceTablaPrincipal);
			
			String salidaPrimerCorte = "";
			salidaPrimerCorte=salidaXML.substring(indiceTablaPrincipal,indiceFinalTablaPrincipal);
			System.out.println(salidaPrimerCorte);
			
			String[] listadoTR = salidaPrimerCorte.split("</tr>");
			Parser parseVehicle = new ParserVehicle();
			
			//Vehicle vehicle = parseVehicle.parse(listadoTR);
			
			ServletOutputStream salida = response.getOutputStream();
			salida.println(salidaPrimerCorte.toString());
		}
		else{
		System.out.println("ERROR : No se encontraron Vehiculos");
		ServletOutputStream salida = response.getOutputStream();
		salida.println("ERROR".toString());
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}
